package org.bugjail.dataaccess.jdbc.objects;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.bugjail.data.Condition;
import org.bugjail.dataaccess.jdbc.connection.QueryException;
import org.bugjail.dataaccess.jdbc.connection.QueryManager;
import org.bugjail.dataaccess.objects.ConditionDAO;

/**
 * @author Felipe Melo
 *
 */
class JDBCConditionDAO implements ConditionDAO {
	
	private final String PROPERTIES_FILE = "/org/bugjail/dataaccess/jdbc/properties/conditiondao.properties";
	
	private final String PROPERTY_INSERT = "insert";
	
	private final String PROPERTY_UPDATE = "update";
	
	private final String PROPERTY_DELETE = "delete";
	
	private final String PROPERTY_RETRIEVE_BY_ID = "retrieve.byid";
	private final String PROPERTY_RETRIEVE_ALL = "retrieve.all";
	private final String PROPERTY_RETRIEVE_BY_NAME = "retrieve.byname";

	@Override
	public void create(Condition element) {
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_INSERT);
				statement.setString(1, element.getDescription());
				statement.executeUpdate();
				
				QueryManager.commit(queryManager);
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			QueryManager.rollback(queryManager);
			throw new QueryException(e.getMessage());
		}
	}

	@Override
	public void delete(Condition element) {
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_DELETE);
				statement.setInt(1, element.getId());
				statement.executeUpdate();
				QueryManager.commit(queryManager);
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			QueryManager.rollback(queryManager);
			throw new QueryException(e.getMessage());
		}
	}

	@Override
	public List<Condition> retrieveAll() {
		List<Condition> conditions = new LinkedList<Condition>();
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_RETRIEVE_ALL);
				ResultSet rs = statement.executeQuery();
				while (rs.next()) {
					conditions.add(extractConditionsFromResultSet(rs, queryManager));
				}
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			throw new QueryException(e.getMessage());
		}
		return conditions;
	}

	@Override
	public Condition retrieveById(int id) {
		Condition condition = new Condition();
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_RETRIEVE_BY_ID);
				statement.setInt(1, id);
				ResultSet rs = statement.executeQuery();
				if (rs.next()) {
					condition = extractConditionsFromResultSet(rs, queryManager);
				}
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			throw new QueryException(e.getMessage());
		}
		return condition;
	}

	@Override
	public List<Condition> retrieveByName(String description) {
		description = "%"+description+"%";
		List<Condition> conditions = new LinkedList<Condition>();
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_RETRIEVE_BY_NAME);
				statement.setString(1, description);
				ResultSet rs = statement.executeQuery();
				while (rs.next()) {
					conditions.add(extractConditionsFromResultSet(rs, queryManager));
				}
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			throw new QueryException(e.getMessage());
		}
		return conditions;
	}

	@Override
	public void update(Condition element) {
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_UPDATE);
				statement.setString(1, element.getDescription());
				statement.setInt(2, element.getId());
				statement.executeUpdate();
				
				QueryManager.commit(queryManager);
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			QueryManager.rollback(queryManager);
			throw new QueryException(e.getMessage());
		}
	}
	
	private Condition extractConditionsFromResultSet(ResultSet rs,
			QueryManager queryManager) throws SQLException {
		Condition condition = new Condition();
		condition.setId(rs.getInt("id"));
		condition.setDescription(rs.getString("description"));
		return condition;
	}

}